package io.bdmc.modules.sys.service.impl;

import io.bdmc.common.annotation.DataFilter;
import io.bdmc.common.utils.MyPage;
import io.bdmc.modules.sys.dao.SysUserMapper;
import io.bdmc.modules.sys.entity.SysUser;
import io.bdmc.modules.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


/**
 * 系统用户
 * 
 * @author huang lu
 * @email hlkyo@qq.com
 * @date 2016年9月18日 上午9:46:09
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
	@Autowired
	private SysUserMapper userMapper;
	

	@Override
	public List<String> queryAllPerms(Long userId) {
		return userMapper.queryAllPerms(userId);
	}

	@Override
	public List<Long> queryAllMenuId(Long userId) {
		return userMapper.queryAllMenuId(userId);
	}

	@Override
	public SysUser queryByUserName(String username) {
		QueryWrapper<SysUser> w1 = new QueryWrapper<SysUser>().eq("username", username);
		return this.getOne(w1);
	}
	


	@Override
	@DataFilter(tableAlias = "d", user = false)
	public MyPage<SysUser> queryList(MyPage<SysUser> myPage,String username){
		return userMapper.queryList(myPage,username);
	}


	@Override
	public Boolean updatePassword(Long userId, String password, String newPassword) {
		UpdateWrapper<SysUser> ew = new UpdateWrapper<SysUser>();
		ew.set("password",newPassword).eq("user_id",userId).eq("password", password);
		SysUser u = new SysUser();
		u.setPassword(newPassword);
		return this.update(u,ew);
	}

	@Override
	public Boolean resetPassword(Long userId,  String newPassword) {
		UpdateWrapper<SysUser> ew = new UpdateWrapper<SysUser>();
		ew.set("password",newPassword).eq("user_id",userId);
		return this.update(
			new SysUser().setPassword(newPassword),
			new QueryWrapper<SysUser>().lambda().eq(SysUser::getUserId, userId)
	);

	}

	@Override
	public List<String> queryNameByDepIds(List<Long> ids) {
		QueryWrapper<SysUser> w1= new QueryWrapper<SysUser>();
		w1.in("dept_id", ids);
		List<SysUser> list =  this.list(w1);
		List<String> result = list.stream().map(s->s.getUsername()).collect(Collectors.toList());
		return result;
	}


	@Override
	public SysUser queryByOpenid(String openid) {
		QueryWrapper<SysUser> w1 = new QueryWrapper<SysUser>().eq("openid", openid);
		return this.getOne(w1);
	}

	@Override
	public SysUser QueryObject(Long userId) {
		return userMapper.queryObject(userId);
	}
}
